package hl.action;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ModelDriven;

import hl.pageModel.DataGrid;
import hl.pageModel.Json;
import hl.pageModel.Training;
import hl.service.TrainingService;


@Namespace("/")
@Action(value = "trainingAction")
public class TrainingAction extends BaseAction implements ModelDriven<Training> {

	Training training = new Training();

	private TrainingService trainingService;

	@Override
	public Training getModel() {
		return training;
	}

	public TrainingService getTrainingService() {
		return trainingService;
	}

	@Autowired
	public void setTrainingService(TrainingService trainingService) {
		this.trainingService = trainingService;
	}

	public void add() {
		Json j = new Json();
		try {
			trainingService.save(training);
			j.setSuccess(true);
			j.setMsg("保存成功");

		} catch (Exception e) {
			j.setMsg(e.getMessage());
		}
		super.writeJson(j);
	}

	public void getDatagrid() {
		DataGrid d = trainingService.getDatagrid(training);
		super.writeJson(d);
	}

	public void edit() {
		Json j = new Json();
		try {
			trainingService.edit(training);
			j.setSuccess(true);
			j.setMsg("修改成功！");

		} catch (Exception e) {
			j.setMsg(e.getMessage());
		}
		super.writeJson(j);
	}

	public void remove() {
		Json j = new Json();
		try {
			trainingService.remove(training);
			j.setSuccess(true);
			j.setMsg("删除成功！");

		} catch (Exception e) {
			j.setMsg(e.getMessage());
		}
		super.writeJson(j);
	}
}
